﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using CommunityMegaphone.ViewModels;

namespace CommunityMegaphone.Views
{
    public partial class CitiesView : PhoneApplicationPage
    {
        public CitiesView()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if ((object)DataContext == null)
                DataContext = App.CitiesViewModel;
        }

        private void OnSelectedCityChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e == null || e.AddedItems == null || e.AddedItems.Count == 0)
                return;

            CityViewModel city = e.AddedItems[0] as CityViewModel;
            if (city == null)
                return;

            // save the selected city info, run the search and navigate back
            SearchResultsViewModel search = App.SearchResultsViewModel;
            search.SearchType = SearchType.WellKnownCity;
            search.SearchCityName = city.Name;
            search.ReloadEvents(new GeoCoordinate(city.Lat, city.Lng));
            NavigationService.GoBack();
        }

        private void OnFilterTextChanged(object sender, TextChangedEventArgs e)
        {
            App.CitiesViewModel.Filter = tbFilter.Text;
        }
    }
}